
export default {
  props: ['info','acticeDay'],
  data() {
    return {
      // ------------------------
      weekList: ['一', '二', '三', '四', '五', '六', '日'], // 周列表
      monthDayList: [], // 当月的日列表
      currentYear: '', // 当前年
      currentMonth: '', // 当前月
      currentDay: '', // 当前日
      today: '', // 今日
      selectedDate: '',
      formatSignedList: [], // 将签到日期格式化
      isDisabledNextMonth: true, // 是否能跳转下一个月
      is_active_start:'',
      is_active_end:'',
    };
  },
  watch: {
    info:{
      handler:function(val,oval){
        this.is_active_start = new Date(val.active_start).getTime();
        this.is_active_end = new Date(val.active_end).getTime();
        if(this.acticeDay){
          this.initCurrentDate(new Date(this.acticeDay));
        }else{
          this.initCurrentDate();
        }
      },
      immediated:true
    }
  },
  methods: {
    tapDay(item) {
      console.log(new Date().getTime(),item.time)
      if(this.is_active_start > item.time || this.is_active_end < item.time){
        return
      }
      this.$emit('postChild',item)
      this.today = item.date;
    },
    formatZero(number) {
      return number;
    },
    //初始化当前天数
    initCurrentDate(date) {
      this.currentDate = date || new Date();
      this.currentYear = this.currentDate.getFullYear();
      this.currentMonth = this.currentDate.getMonth() + 1;
      this.currentDay = this.currentDate.getDate();
      // 传的日期为空默认今日
      let day,month;
      month = this.currentMonth < 10?`0${this.currentMonth}`:this.currentMonth;
      day = this.currentDay < 10?`0${this.currentDay}`:this.currentDay;
      // if (!date) {
        this.today = `${this.currentYear}-${month}-${day}`;
        this.days = this.currentDay;
      // }
      this.initMonthDayList();
    },
    //初始化具体日期
    initMonthDayList() {
      // 获取当月1号是周几
      let date = new Date(this.currentYear, this.currentMonth - 1, 1);
      let weekDay = date.getDay();

      // 获取当月的总天数
      let monthFirstDay = new Date(this.currentYear, this.currentMonth, 0);
      let monthDay = monthFirstDay.getDate();

      let list = [];
      if(weekDay == 0){
        for (let index = 0; index < 6; index++) {
          list.push(null);
        }
      }else{
        for (let index = 0; index < weekDay - 1; index++) {
          list.push(null);
        }
      }
      for (let index = 1; index <= monthDay; index++) {

        if (this.currentMonth >= 1 && this.currentMonth < 10) {
          if (index >= 1 && index < 10) {
            const itemDate = `${this.currentYear}-0${this.currentMonth}-0${index}`;
            let item = {
              day: index,
              date: itemDate, // 具体日期
              time: new Date(`${this.currentYear}-0${this.currentMonth}-0${index}`).getTime()
            };
            list.push(item);
          } else {
            const itemDate = `${this.currentYear}-0${this.currentMonth}-${index}`;
            let item = {
              day: index,
              date: itemDate, // 具体日期
              time: new Date(`${this.currentYear}-0${this.currentMonth}-${index}`).getTime()
            };
            list.push(item);
          }
        }
        else if (this.currentMonth >= 10) {
          if (index >= 1 && index < 10) {
            const itemDate = `${this.currentYear}-${this.currentMonth}-0${index}`;
            let item = {
              day: index,
              date: itemDate, // 具体日期
              time: new Date(`${this.currentYear}-${this.currentMonth}-0${index}`).getTime()
            };
            list.push(item);
          } else {
            const itemDate = `${this.currentYear}-${this.currentMonth}-${index}`;
            let item = {
              day: index,
              date: itemDate, // 具体日期
              time: new Date(`${this.currentYear}-${this.currentMonth}-${index}`).getTime()
            };
            list.push(item);
          }

        }
      }
      this.monthDayList = list;
      console.log(this.today)
    },
    //上一月
    handlePreMonth() {
      this.handleToggleMonth(-1);
      // this.getnewDate();
      //
    },
    //下一月
    handleNextMonth() {
      this.handleToggleMonth(1);
      // this.getnewDate();
    },
    // 切换月
    handleToggleMonth(data) {
      let month = this.currentMonth += data;
      let year = this.currentYear;
      if (month <= 0) {
        month = 12;
        year--;
      } else if (month > 12) {
        month = 1;
        year++;
      }
      this.currentYear = year,
      this.currentMonth = month;
      this.initMonthDayList();
      // this.triggerEvent('change', { date: `${year}/${month}` });
    },
    isLastDay(time) {
      return this.is_active_end < time || this.is_active_start > time ? true : false;
    },
    //判断是否为签到日期
    isSigned(date, list=[]) {
      if (list.indexOf(date) > -1) {
        return true;
      }
      return false;
    },
    // 任务期间判断
    dayOn(date, list=[]){
      if (list.indexOf(date) > -1) {
        return false;
      }
      if(new Date(date).getTime() < this.is_active_start) return false
      if(new Date(date).getTime() > this.is_active_end) return false
      return true;
    },
    // 显示开始日期
    isNoSigned(date, list=[]){
      if(new Date(date).getTime() == this.is_active_start) return true
      return false
    },
    //显示结束日期
    isNoEnd(date, list=[]){
      if(new Date(date).getTime() == this.is_active_end) return true
      return false
    }
  },
  computed: {
  },
  created() {
    // this.initCurrentDate();
  },
  mounted() {
  }

};